package com.coolbi.security.web.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.common.web.action.BaseAction;
import com.coolbi.security.biz.RoleToGroupBiz;
import com.coolbi.security.biz.RoleToUrlBiz;
import com.coolbi.security.biz.RoleToUserBiz;
import com.coolbi.security.biz.RoleUrlBiz;
import com.coolbi.security.biz.RolesBiz;
import com.coolbi.security.biz.UserBiz;
import com.coolbi.security.biz.UserGroupBiz;
import com.coolbi.security.biz.UserToGroupBiz;
import com.coolbi.security.entity.RoleToGroup;
import com.coolbi.security.entity.RoleToUrl;
import com.coolbi.security.entity.RoleToUser;
import com.coolbi.security.entity.RoleUrl;
import com.coolbi.security.entity.Roles;
import com.coolbi.security.entity.User;
import com.coolbi.security.entity.UserGroup;

/** 授权Action */
public class AuthorAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private User user;
	private RoleToUser roleToUser;
	private RoleToUrl roleToUrl;
	private Roles role;
	private UserGroup userGroup;
	private RoleToGroup roleToGroup;
	private List<User> userList;
	/**所有角色*/
	private List<Roles> rolesList;
	/**某一用户组拥有的角色*/
	private List<Roles> groupRoles;
	/**某一用户拥有的角色*/
	private List<Roles> roles;
	private List<UserGroup> userGroupList;
	/**所有资源*/
	private List<RoleUrl> urlList;
	/**某一角色拥有的资源*/
	private List<RoleUrl> urls;
	private Long[] roleArray;
	private Long[] authArray;
	private Long[] abolishArray;
	
	@Autowired
	private UserBiz userBiz;
	public void setUserBiz(UserBiz userBiz) {
		this.userBiz = userBiz;
	}
	
	@Autowired
	private RolesBiz rolesBiz;
	public void setRolesBiz(RolesBiz rolesBiz) {
		this.rolesBiz = rolesBiz;
	}
	
	@Autowired
	private UserToGroupBiz userToGroupBiz;
	public void setUserToGroupBiz(UserToGroupBiz userToGroupBiz) {
		this.userToGroupBiz = userToGroupBiz;
	}
	
	@Autowired
	private RoleToUserBiz roleToUserBiz;
	public void setRoleToUserBiz(RoleToUserBiz roleToUserBiz) {
		this.roleToUserBiz = roleToUserBiz;
	}
	
	@Autowired
	private RoleToUrlBiz roleToUrlBiz;
	public void setRoleToUrlBiz(RoleToUrlBiz roleToUrlBiz) {
		this.roleToUrlBiz = roleToUrlBiz;
	}
	
	@Autowired
	private RoleUrlBiz roleUrlBiz;
	public void setRoleUrlBiz(RoleUrlBiz roleUrlBiz) {
		this.roleUrlBiz = roleUrlBiz;
	}
	
	@Autowired
	private UserGroupBiz userGroupBiz;
	public void setUserGroupBiz(UserGroupBiz userGroupBiz) {
		this.userGroupBiz = userGroupBiz;
	}
	
	@Autowired
	private RoleToGroupBiz roleToGroupBiz;
	public void setRoleToGroupBiz(RoleToGroupBiz roleToGroupBiz) {
		this.roleToGroupBiz = roleToGroupBiz;
	}

	/**获取管理员信息、用户组信息、所有角色*/
	public String findUserAuthoById() {
		try {
			user = userBiz.get(user.getId());
			userGroupList = userToGroupBiz.findGroupByUserId(user.getId());
			pager.setRecordCount(rolesBiz.count());
			rolesList = rolesBiz.list((pager.getCurrPageNo() - 1) * pager.getPageSize(), pager.getPageSize());
			roles = roleToUserBiz.findRoleByUserId(user.getId());
			groupRoles = roleToUserBiz.findGroupRoleByUserId(user.getId());
			roles.addAll(groupRoles);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**为管理员授以各种角色*/
	public String authoRoleToUser() throws Exception {
		try {
			roleToUserBiz.saveRoleAuthToUser(authArray, abolishArray, roleToUser);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**获取角色信息、所有资源信息*/
	public String findRoleAuthById() throws Exception {
		role = rolesBiz.get(role.getId());
		urlList = roleUrlBiz.list();
		urls = roleToUrlBiz.findUrlByRoleId(role.getId());
		return SUCCESS;
	}
	
	/**为角色授以各种资源*/
	public String authoUrlToRole() throws Exception {
		roleToUrlBiz.saveUrlAuthToRole(authArray, abolishArray, roleToUrl);
		return SUCCESS;
	}
	
	/**获取用户组、所有角色信息*/
	public String findRoleAuthByGroupId() {
		try {
			userGroup = userGroupBiz.get(userGroup.getId());
			pager.setRecordCount(rolesBiz.count());
			rolesList = rolesBiz.list((pager.getCurrPageNo() - 1) * pager.getPageSize(), pager.getPageSize());
			roles = roleToGroupBiz.findRoleByGroupId(userGroup.getId());
			groupRoles = roleToGroupBiz.findParentRoleByGroupId(userGroup.getId());
			roles.addAll(groupRoles);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**为用户组授以各种角色*/
	public String authoRoleToGroup() throws Exception {
		roleToGroupBiz.saveRoleAuthToGroup(authArray, abolishArray, roleToGroup);
		return SUCCESS;
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<UserGroup> getUserGroupList() {
		return userGroupList;
	}

	public void setUserGroupList(List<UserGroup> userGroupList) {
		this.userGroupList = userGroupList;
	}

	public List<Roles> getRolesList() {
		return rolesList;
	}

	public void setRolesList(List<Roles> rolesList) {
		this.rolesList = rolesList;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public List<Roles> getRoles() {
		return roles;
	}

	public void setRoles(List<Roles> roles) {
		this.roles = roles;
	}

	public Long[] getRoleArray() {
		return roleArray;
	}

	public void setRoleArray(Long[] roleArray) {
		this.roleArray = roleArray;
	}

	public RoleToUser getRoleToUser() {
		return roleToUser;
	}

	public void setRoleToUser(RoleToUser roleToUser) {
		this.roleToUser = roleToUser;
	}

	public Roles getRole() {
		return role;
	}

	public void setRole(Roles role) {
		this.role = role;
	}

	public List<RoleUrl> getUrlList() {
		return urlList;
	}

	public void setUrlList(List<RoleUrl> urlList) {
		this.urlList = urlList;
	}

	public List<RoleUrl> getUrls() {
		return urls;
	}

	public void setUrls(List<RoleUrl> urls) {
		this.urls = urls;
	}

	public RoleToUrl getRoleToUrl() {
		return roleToUrl;
	}

	public void setRoleToUrl(RoleToUrl roleToUrl) {
		this.roleToUrl = roleToUrl;
	}

	public UserGroup getUserGroup() {
		return userGroup;
	}

	public void setUserGroup(UserGroup userGroup) {
		this.userGroup = userGroup;
	}

	public RoleToGroup getRoleToGroup() {
		return roleToGroup;
	}

	public void setRoleToGroup(RoleToGroup roleToGroup) {
		this.roleToGroup = roleToGroup;
	}

	public List<Roles> getGroupRoles() {
		return groupRoles;
	}

	public void setGroupRoles(List<Roles> groupRoles) {
		this.groupRoles = groupRoles;
	}

	public Long[] getAuthArray() {
		return authArray;
	}

	public void setAuthArray(Long[] authArray) {
		this.authArray = authArray;
	}

	public Long[] getAbolishArray() {
		return abolishArray;
	}

	public void setAbolishArray(Long[] abolishArray) {
		this.abolishArray = abolishArray;
	}
}
